package com.cloud.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.cloud.train.business.domain.Station;
import com.cloud.train.business.domain.StationExample;
import com.cloud.train.business.mapper.StationMapper;
import com.cloud.train.business.req.StationQueryReq;
import com.cloud.train.business.req.StationSaveReq;
import com.cloud.train.business.resp.StationQueryResp;
import com.cloud.train.common.exception.BusinessException;
import com.cloud.train.common.exception.BusinessExceptionEnum;
import com.cloud.train.common.resp.PageResp;
import com.cloud.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StationService {
    @Resource
    private StationMapper stationMapper;
    public void save(StationSaveReq req) {
        DateTime dateTime = new DateTime();
        Station station = BeanUtil.copyProperties(req, Station.class);
        if (ObjectUtil.isNull(station.getId())){
            Station name = selectByUnique(req.getName());
            if (ObjectUtil.isNotEmpty(name)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_NAME_UNIQUE_ERROR);
            }
            station.setCreateTime(dateTime);
            station.setUpdateTime(dateTime);
            station.setId(SnowUtil.getSnowflakeNextId());
            stationMapper.insert(station);
        }
        else {
            station.setUpdateTime(dateTime);
            stationMapper.updateByPrimaryKey(station);
        }
    }
    private Station selectByUnique(String name){
        StationExample stationExample = new StationExample();
        stationExample.createCriteria().andNameEqualTo(name);
        List<Station> stations = stationMapper.selectByExample(stationExample);
        if (CollUtil.isNotEmpty(stations)){
            return stations.get(0);
        }
        else {
            return null;
        }
    }

    public PageResp<StationQueryResp> queryList(StationQueryReq req) {
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("id desc");
        StationExample.Criteria criteria = stationExample.createCriteria();
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Station> stations = stationMapper.selectByExample(stationExample);
        PageInfo<Station> stationPageInfo = new PageInfo<>(stations);
        List<StationQueryResp> stationQueryResps = BeanUtil.copyToList(stations, StationQueryResp.class);
        PageResp<StationQueryResp> objectPageResp = new PageResp<StationQueryResp>();
        objectPageResp.setTotal(stationPageInfo.getTotal());
        objectPageResp.setList(stationQueryResps);
        return objectPageResp;
    }

    public void delete(long id) throws Exception {
        int i = stationMapper.deleteByPrimaryKey(id);
        if (i == 0){
            throw new Exception("删除失败未知错误");
        }
    }

    public List<StationQueryResp> queryAll() {
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("name_pinyin asc");
        List<Station> stationList = stationMapper.selectByExample(stationExample);
        return BeanUtil.copyToList(stationList, StationQueryResp.class);
    }
}
